package com.atguigu.service.Impl;

import com.atguigu.dao.BookDao;
import com.atguigu.dao.Impl.BookDaoImpl;
import com.atguigu.dao.Impl.OrderDaoImpl;
import com.atguigu.dao.Impl.OrderItemDaoImpl;
import com.atguigu.dao.OrderDao;
import com.atguigu.dao.OrderItemDao;
import com.atguigu.pojo.*;
import com.atguigu.service.OrderService;
import com.atguigu.utils.WebUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName OrderServiceImpl
 * @Description TODO
 * @Author 周轩霆
 * @Date 2022/4/28 19:37
 **/

public class OrderServiceImpl implements OrderService {
    private OrderDao orderDao = new OrderDaoImpl();
    private OrderItemDao orderItemDao = new OrderItemDaoImpl();
    private BookDao bookDao = new BookDaoImpl();//修改库存和销量

    @Override
    public String createOrder(Cart cart, Integer userId) {
        //先保存订单
        //订单号，唯一性 时间戳＋用户id
        String orderId = System.currentTimeMillis() + "" + userId;
        Order order = new Order(orderId, new Date(), cart.getTotalPrice(), 0, userId);
        orderDao.saveOrder(order);
        //保存订单项==>就是商品项,CartItem中的商品项
        Map<Integer, CartItem> items = cart.getItems();
        for (Map.Entry<Integer, CartItem> entry : items.entrySet()) {
            //获取每一个购物车的商品项
            CartItem cartItem = entry.getValue();
            //转换为每一个订单项
            OrderItem orderItem = new OrderItem(null, cartItem.getName(), cartItem.getCount(), cartItem.getPrice(), cartItem.getTotalPrice(), orderId);
            //保存到数据库中
            orderItemDao.saveOrderItem(orderItem);
            //更新库存和销量
            Book book = bookDao.queryBookById(cartItem.getId());
            book.setSales(book.getSales() + cartItem.getCount());
            book.setStock(book.getStock() - cartItem.getCount());
            bookDao.updateBook(book);
        }
        //结账之后清空购物车
        cart.clear();
        //返回orderId
        return orderId;

    }

    /**
     * @return java.util.List<com.atguigu.pojo.Order>
     * @Author 周轩霆
     * @Description 查询所有订单
     * @Date 14:56 2022/4/29
     * @Param []
     */
    @Override
    public List<Order> showAllOrders() {
        return orderDao.queryOrders();
    }

    /**
     * @return com.atguigu.pojo.Page<com.atguigu.pojo.Order>
     * @Author 周轩霆
     * @Description 订单的分页
     * @Date 15:30 2022/4/29
     * @Param [pageNo, pageSize]
     */
    @Override
    public Page<Order> page(int pageNo, int pageSize) {
        Page<Order> page = new Page<>();

       /* //对属性一一进行赋值

        page.setPageSize(pageSize);
        //设置总记录数
        Integer pageTotalCount = bookDao.queryForPageTotalCount();
        page.setPageTotalCount(pageTotalCount);
        //设置总页码数
        Integer pageTotal = pageTotalCount / pageSize;
        if (pageTotalCount % pageSize > 0) {
            pageTotal++;
        }
        *//*   数据边界的有效检查 注意顺序         *//*
        page.setPageTotal(pageTotal);

        page.setPageNo(pageNo);
        //设置当前页的数据
        int begin = (page.getPageNo() - 1) * pageSize;
        List<Book> items = bookDao.queryForPageItems(begin, pageSize);
        page.setItems(items);
        return page;*/
        return null;
    }

    /**
     * @return int
     * @Author 周轩霆
     * @Description 用于处理发货请求
     * @Date 16:38 2022/4/30
     * @Param [orderId]
     */
    @Override
    public int sendOrder(String orderId) {
        //将状态改为已发货

        return orderDao.changeOrderStatus(orderId, 1);

    }
}
